package com.zhiyun.test.base.element;

import com.zhiyun.test.base.util.PropUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.util.Objects;

/**
 * Created by user on 2019-5-27.
 */
public class BaseElement {

  public WebDriver webDriver;
  public Actions actions;
  public BaseElement(WebDriver webDriver){
    this.webDriver = webDriver;
    this.actions = new Actions(webDriver);
  }

  public WebElement getElement(String name) {
    return getElement(PropUtil.getProp("site.prop",name+"Element"),PropUtil.getProp("site.prop",name+"Local"));
  }

  public WebElement getElement(String selector, String location){
    return getElement(getBySelector(selector, location));
  }

  public WebElement getElement(By by){
    return webDriver.findElement(by);
  }

  public By getBySelector(String selector,String location){
    By by = null;
    if(Objects.equals(selector,"id")){
      by = By.id(location);
    }else if(Objects.equals(selector,"className")){
      by = By.className(location);
    }else if(Objects.equals(selector,"xpath")){
      by = By.xpath(location);
    }else if(Objects.equals(selector,"tagName")){
      by = By.tagName(location);
    }else if(Objects.equals(selector,"name")){
      by = By.name(location);
    }else if(Objects.equals(selector,"linkText")){
      by = By.linkText(location);
    }else if(Objects.equals(selector,"partialLinkText")){
      by = By.partialLinkText(location);
    }
    return by;
  }
  public void sendKeys(String element,String key){
    getElement(element).sendKeys(key);
  }

  public void click(String element) {
    getElement(element).click();
  }

  public void moveTo(String element){
    actions.moveToElement(getElement(element));
  }
}
